package org.jeik.beackBeans.admin;

import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import javax.annotation.PostConstruct;
import javax.faces.application.FacesMessage;
import javax.faces.model.SelectItem;

import org.jeik.beackBeans.BeansBase;
import org.jeik.common.CustomerRequestStatus;
import org.jeik.common.ServiceType;
import org.jeik.controllers.MsgsController;
import org.jeik.controllers.NavigationController;
import org.jeik.controllers.NavigationRule;
import org.jeik.data.dao.DataException;
import org.jeik.data.models.CustomerRequest;
import org.jeik.data.services.CustomerRequestsService;

@SuppressWarnings("serial")
public class CustomerRequestsHandlerBean extends BeansBase {

	private List<CustomerRequest> customerRequestsList;
	private CustomerRequest request;
	private List<SelectItem> statusesList;
	private List<SelectItem> serviceTypesList;
	private String firstName;
	private String lastName;
	private String mobileNumber;
	
	@PostConstruct
	public void init(){
		
		loadServicesTypes();
		loadCustomerRequestsStatuses();
		
	}

	public void preEditCustomerRequest(CustomerRequest request) {
		this.request = request;
		NavigationController.navigate(NavigationRule.EDIT_CUSTOMER_REQUEST);
	}

	public void preAddRequest() throws DataException {
		request = new CustomerRequest();
		request.setApplyDate(new Date());
		request.setStatus(Long.parseLong(String.valueOf(CustomerRequestStatus.PENDING.getValue())));
		NavigationController
				.navigate(NavigationRule.ADD_CUSTOMER_REQUEST);
	}

	/**
	 * @throws DataException
	 * 
	 */
	public void addCustomerRequest() throws DataException {

		request.setCreatedDate(new Date());
		request.setLastUpdatedDate(new Date());
		CustomerRequestsService.getInstance().addCustomerRequest(
				request);
		MsgsController.addFacesGlobalMessage(FacesMessage.SEVERITY_INFO,
				"تم اضافة طلب المراجع بنجاح");
		
		customerRequestsList = CustomerRequestsService.getInstance()
				.loadCustomerRequests();

		NavigationController
				.navigate(NavigationRule.CUSTOMERS_REQUESTS_SUMMARY);
	}
	
	public void editCustomerRequest() throws DataException {
		
		request.setLastUpdatedDate(new Date());
		CustomerRequestsService.getInstance().editCustomerRequest(
				request);
		MsgsController.addFacesGlobalMessage(FacesMessage.SEVERITY_INFO,
				"تم تعديل طلب المراجع بنجاح");
		
		customerRequestsList = CustomerRequestsService.getInstance()
				.loadCustomerRequests();

		NavigationController
				.navigate(NavigationRule.CUSTOMERS_REQUESTS_SUMMARY);
	}
	

	public void loadCustomerRequestsSummary() throws DataException {

		firstName = "";
		lastName = "";
		mobileNumber = "";  
		
		//if (customerRequestsList == null || customerRequestsList.size() == 0) {
			customerRequestsList = CustomerRequestsService.getInstance()
					.loadCustomerRequests();
		//}

		NavigationController.navigate(NavigationRule.CUSTOMERS_REQUESTS_SUMMARY);
	}

	public CustomerRequest getRequest() {
		return request;
	}

	public void setRequest(CustomerRequest request) {
		this.request = request;
	}

	public List<CustomerRequest> getCustomerRequestsList() {
		return customerRequestsList;
	}

	public void setCustomerRequestsList(
			List<CustomerRequest> customerRequestsList) {
		this.customerRequestsList = customerRequestsList;
	}
	
	public void loadServicesTypes(){
		Map<String,ServiceType> serviceTypesMap = ServiceType.getLookup();
		serviceTypesList = new ArrayList<SelectItem>();
		Iterator<Entry<String, ServiceType>> iterator = serviceTypesMap.entrySet().iterator();
		while (iterator.hasNext()) {
			
			Map.Entry<String, ServiceType> mapEntry = iterator.next();
			SelectItem serviceTypeItem = new SelectItem();
			serviceTypeItem.setLabel(mapEntry.getValue().getLabel());
			serviceTypeItem.setValue(mapEntry.getValue().getValue());
			serviceTypesList.add(serviceTypeItem);
		}
	}
	
	public void loadCustomerRequestsStatuses(){
		Map<String,CustomerRequestStatus> statusesMap = CustomerRequestStatus.getLookup();
		statusesList  = new ArrayList<SelectItem>();
		Iterator<Entry<String, CustomerRequestStatus>> iterator = statusesMap.entrySet().iterator();
		while (iterator.hasNext()) {
			
			Map.Entry<String, CustomerRequestStatus> mapEntry = iterator.next();
			SelectItem statusItem = new SelectItem();
			statusItem.setLabel(mapEntry.getValue().getLabel());
			statusItem.setValue(mapEntry.getValue().getValue());
			statusesList.add(statusItem);
		}
	}
	
	public void filterCustomerRequests(){
		customerRequestsList = new ArrayList<CustomerRequest>();
		try {
			customerRequestsList = CustomerRequestsService.getInstance().filterCustomersRequests(firstName,lastName,mobileNumber);
		} catch (DataException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public String getFirstName() {
		return firstName;
	}

	public void setFirstName(String firstName) {
		this.firstName = firstName;
	}

	public String getLastName() {
		return lastName;
	}

	public void setLastName(String lastName) {
		this.lastName = lastName;
	}

	public String getMobileNumber() {
		return mobileNumber;
	}

	public void setMobileNumber(String mobileNumber) {
		this.mobileNumber = mobileNumber;
	}
	
	public List<SelectItem> getStatusesList() {
		return statusesList;
	}

	public void setStatusesList(List<SelectItem> statusesList) {
		this.statusesList = statusesList;
	}

	public List<SelectItem> getServiceTypesList() {
		return serviceTypesList;
	}

	public void setServiceTypesList(List<SelectItem> serviceTypesList) {
		this.serviceTypesList = serviceTypesList;
	}

}
